package Thread2;

public class Homework4 {

    public static void main4(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();

        Thread t1 = new Thread(() -> {
            // 尝试拿起一把锁
            synchronized (locker1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // 尝试拿起另一把锁
                synchronized (locker2){
                    System.out.println("t1 线程两把锁都拿到了");
                }
            }
        });

        Thread t2 = new Thread(() -> {
            // 尝试拿起一把锁
            synchronized (locker2){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // 尝试拿起另一把锁
                synchronized (locker1){
                    System.out.println("t2 线程两把锁都拿到了");
                }
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();


    }

    public static void main(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Object locker3 = new Object();

        Thread a = new Thread(() -> {
            try {
                synchronized (locker1) {
                    locker1.wait();
                }
                System.out.print(Thread.currentThread().getName());
                synchronized (locker3) {
                    locker3.notify();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"a");

        Thread b = new Thread(() -> {
            try {
                synchronized (locker2) {
                    locker2.wait();
                }
                System.out.print(Thread.currentThread().getName() + ",");
                synchronized (locker1) {
                    locker1.notify();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"b");

        Thread c = new Thread(() -> {
            try {
                synchronized (locker3) {
                    locker3.wait();
                }
                System.out.print(Thread.currentThread().getName() + ",");
                synchronized (locker2) {
                    locker2.notify();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"c");

        a.start();
        b.start();
        c.start();

        Thread.sleep(1000);
        synchronized (locker3){
            locker3.notify();
        }
    }
    public static void main2(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Object locker3 = new Object();

        Thread t1 = new Thread(() -> {

                try{
                    for (int i = 0; i < 10; i++) {
                        synchronized (locker1){
                            locker1.wait();
                        }
                        System.out.print("A");
                        synchronized (locker2){
                            locker2.notify();
                        }
                    }

                }catch (InterruptedException e){
                    throw new RuntimeException();
                }

        });

        Thread t2 = new Thread(() -> {
                try{
                    for (int i = 0; i < 10; i++) {
                        synchronized (locker2){
                            locker2.wait();
                        }
                        System.out.print("B");
                        synchronized (locker3){
                            locker3.notify();
                        }
                    }
                }catch (InterruptedException e){
                    throw new RuntimeException();
                }
        });

        Thread t3 = new Thread(() -> {

                try{
                    for (int i = 0; i < 10; i++) {
                        synchronized (locker3){
                            locker3.wait();
                        }
                        System.out.println("C");
                        synchronized (locker1){
                            locker1.notify();
                        }
                    }
                }catch (InterruptedException e){
                    throw new RuntimeException();
                }
        });

        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(1000);
        synchronized (locker1){
            locker1.notify();
        }
    }
    private static int count = 0;
    public static void mai1n(String[] args) throws InterruptedException {
        Object locker = new Object();

        Thread t1 = new Thread(() -> {
            for(int i = 0;i < 10000;i++){
                synchronized (locker){
                    count++;
                }
            }
        });

        Thread t2 = new Thread(() -> {
            for(int i = 0;i < 10000;i++){
                synchronized (locker){
                    count++;
                }
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("count = " + count);
    }
}
